package com.appkubes.fintech.restful.application.repair.service.impl;


import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.common.util.ConversionUtil;
import com.appkubes.fintech.core.po.FintechRepairOrderItem;
import com.appkubes.fintech.core.po.FintechRepairOrderItemExample;
import com.appkubes.fintech.core.pojo.FintechRepairOrderItemMapper;

import com.appkubes.fintech.restful.application.repair.request.RepairOrderItemRequest;
import com.appkubes.fintech.restful.application.repair.response.RepairOrderItemResponse;
import com.appkubes.fintech.restful.application.repair.service.RepairOrderItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/*
 * 订单明细
 * @author    xqc
 * @date    2020/02/05 10:50:56
 */
@Service
@Slf4j
public class RepairOrderItemServiceImpl implements RepairOrderItemService {


    @Resource
    private FintechRepairOrderItemMapper orderItemRepository;


    @Override
    public PageInfo<RepairOrderItemResponse> queryPage(RepairOrderItemRequest repairOrderItemRequest) {
        PageHelper.startPage(repairOrderItemRequest.getPage(), repairOrderItemRequest.getPageSize());
        List<FintechRepairOrderItem> repairOrderItemList = orderItemRepository.selectByExample(new FintechRepairOrderItemExample());
        List<RepairOrderItemResponse> responseList = ConversionUtil.ofList(repairOrderItemList, RepairOrderItemResponse.class);
        return new PageInfo<>(responseList);
    }


    @Override
    public Response save(RepairOrderItemRequest repairOrderItemRequest) {
        FintechRepairOrderItem repairOrderItem = new FintechRepairOrderItem();
        BeanUtils.copyProperties(repairOrderItemRequest, repairOrderItem);
        orderItemRepository.insert(repairOrderItem);
        return Response.success();
    }


    @Override
    public Response update(RepairOrderItemRequest repairOrderItemRequest) {
        if (StringUtils.isBlank(repairOrderItemRequest.getId())) {
            return Response.fail("主键不能为空");
        }
        FintechRepairOrderItem old = orderItemRepository.selectByPrimaryKey(repairOrderItemRequest.getId());
        if (old == null) {
            return Response.fail("对象不存在");
        }
        BeanUtils.copyProperties(repairOrderItemRequest, old, "id", "createTime", "updateTime");
        orderItemRepository.updateByPrimaryKey(old);
        return Response.success();
    }


    @Override
    public Response deleteRepairOrderItem(String id) {
        if (StringUtils.isBlank(id)) {
            return Response.fail("主键不能为空");
        }
        FintechRepairOrderItem repairOrderItem = orderItemRepository.selectByPrimaryKey(id);
        if (repairOrderItem == null) {
            return Response.fail("对象不存在");
        }
        orderItemRepository.deleteByPrimaryKey(repairOrderItem.getId());
        return Response.success();
    }

    @Override
    public Response<RepairOrderItemResponse> getById(String id) {
        FintechRepairOrderItem repairOrderItem = orderItemRepository.selectByPrimaryKey(id);
        RepairOrderItemResponse repairOrderItemResponse = new RepairOrderItemResponse();
        BeanUtils.copyProperties(repairOrderItem, repairOrderItemResponse);
        return Response.success(repairOrderItemResponse);

    }

    @Override
    public void deleteById(String id) {
        orderItemRepository.deleteByPrimaryKey(id);
    }


}
